package com.zhanglijie.improve.aqs.share.semaphore;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/10/4 0004 15:58
 */
public class MySemaphoreTest {
    public static void main(String[] args) {
        MySemaphore semaphore = new MySemaphore(3);
        //  SynchronizedSemaphore semaphore = new SynchronizedSemaphore(3);
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    eat();
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    System.out.println(Thread.currentThread().getName()+"将退出吃饭");
                    semaphore.release();
                }
            },"thread"+i).start();
        }

    }

    private static void eat() throws InterruptedException {
        System.out.println(Thread.currentThread().getName()+"在吃饭");
        Thread.sleep(2000);
    }
}
